home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1039 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  7.2 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: ajay@lehman.com (Ajay Kamdar)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: sample auto_ptr template
  5. Date: 11 Apr 1996 20:36:36 GMT
  6. Organization: Lehman Brothers, Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4kjqam$qs6@jabba.lehman.com>
  9. References: <009A04DA6A831C40.49800EAC@ittpub.nl> <bill-0504961003150001@bgibbons.vip.best.com> <4k4noe$igl@jabba.lehman.com> <KANZE.96Apr9115532@gabi.gabi-soft.fr>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Length: 6463
  12. X-Lines: 160
  13. Originator: clamage@taumet
  14.  
  15. In article <KANZE.96Apr9115532@gabi.gabi-soft.fr>,
  16. J. Kanze <kanze@gabi-soft.fr> wrote:
  17. >In article <4k4noe$igl@jabba.lehman.com> ajay@lehman.com (Ajay Kamdar)
  18. >writes:
  19. >
  20. >|> In article <bill-0504961003150001@bgibbons.vip.best.com>,
  21. >|> Bill Gibbons <bill@gibbons.org> wrote:
  22. >|> >
  23. >|> >Transfer of ownership is not the end goal - the end goal is
  24. >|> >to make auto_ptr useful for the "resource acquisition is
  25. >|> >initialization" idiom.  That is very painful without transfer
  26. >|> >of ownership.
  27. >|> >
  28. >|> >In particular, if you want to do the resource acquisition in a
  29. >|> >function called by the function which needs to hold the resource,
  30. >|> >there is no good exception-safe way to pass the pointer from the
  31. >|> >callee to the caller.
  32. >|> >
  33. >|> >You can get close (at some cost in clarity):
  34. >|> >
  35. >|>        [ snip... ]
  36. >
  37. >|> It is not clear at all that the copy semantics of auto_ptr
  38. >|> are essential for exception-safe transfer of resources.
  39. >|> The same example coded as follows does not use
  40. >|> the copy semantics of auto_ptr:
  41. >
  42. >
  43. >|>     extern X* get_X();   // returns a resource acquired
  44. >|>              // by the callee, to be deleted
  45. >|>              // by the caller.
  46. >
  47. >|>     void f() {
  48. >|>         auto_ptr<X> ptr = get_X();
  49. >|>             // resource allocated by get_X()
  50. >|>         ...
  51. >|>    }
  52. >
  53. >|> And get_X() is not unnecessarily complicated either:
  54. >
  55. >|>    X* get_X()
  56. >|>    {
  57. >|>       auto_ptr<X> p = new X;
  58. >
  59. >|>       // ... stuff that could throw an exception
  60. >
  61. >|>       // We got here. Means normal return.
  62. >|>       return p.release();
  63. >|>    }
  64. >
  65. >
  66. >|> What's wrong with this? It doesn't require copy semantics
  67. >|> for auto_ptr. Yet both the caller and the callee
  68. >|> are exception safe and there is no loss of clarity.
  69. >
  70. >As Bill pointed out in a previous incarnation of this thread, `get_X',
  71. >as written above is *NOT* exception safe.  After the call to p.release,
  72. >the compiler will still cause all of the destructors of any local
  73. >variables (and any value parameters to the function) to be called.  If
  74. >any of these destructors throw an exception, you've lost p.  For good.
  75. >
  76. >Now, I don't generally think it a good idea to let an exception
  77. >propagate out of a destructor.  But I don't think that a standard
  78. >auto_ptr class should require this sort of additional rule in order to
  79. >be useful.
  80.  
  81.  
  82. I don't think a standard auto_ptr class should require additional
  83. rules on the use of const objects (auto_ptrs) in order to be
  84. useful.
  85.  
  86. The currently proposed auto_ptr semantics require additional
  87. rules to be followed by the programmer regarding how the
  88. auto_ptr is used to ensure protection against dangling pointers.
  89. That burden is more onerous by far than any benefits gained by
  90. trying to support the questionable practice of allowing exceptions
  91. to propagate out of destructors.
  92.  
  93. The following example demonstrates why allowing exceptions to
  94. propogate out of destructors is more trouble than it is worth:
  95.  
  96.    auto_ptr<X>  get_X()
  97.    {
  98.       auto_ptr<X> p    = new X;
  99.       auto_ptr<X> temp = new X;
  100.       BadIdea     b1; // throws an exception in dtor
  101.       BadIdea     b2; // throws an exception in dtor
  102.  
  103.       // ... stuff
  104.  
  105.       return p;
  106.    }
  107.  
  108. When b2 is destructed, it throws an exception. So what happens
  109. next?
  110.  
  111. Does the run time system try to continue to destruct b1, temp,
  112. and p? If it does, b1's destructor is also going to thow an
  113. exception. If I interpret 15.5.1 in theApril '95 CD correctly
  114. (I don't have access to any later drafts), terminate() is called
  115. because of the exception propagating out of b1's destructor.
  116. In this case, the destructors of the auto_ptrs would never fire,
  117. making the use of auto_ptrs rather moot.
  118.  
  119. If on the other hand the run time system abandons executing
  120. the other destuctors in get_X() after b2's destructor has
  121. thrown an exception, the destructors of b1, temp, and p would
  122. never be executed. This would in turn mean that the resource
  123. held by temp will never be deleted, making the use of auto_ptr
  124. rather moot.
  125.  
  126. To recap, the only exception handling related situation in which
  127. the copy semantics of auto_ptr can concievably help are related
  128. to exceptions propagating out of destructors. But a program
  129. appears to be either doomed to terminate or to lose resources
  130. in such situations, making the practice of allowing exceptions
  131. to propagate out of destructors to be an extremely poor one.
  132.  
  133. Now I don't think it is a good idea to make the use of auto_ptrs
  134. difficult for normal programming by allowing the possibility
  135. of dangling pointers and such other nasty things to support
  136. the questionable practice of exceptions propagating out of
  137. destructors. Do you?
  138.  
  139. The only situation in which the copy semantics of auto_ptr
  140. do have useful value is in enforcing that resources transferred
  141. from the caller to callee (or vice versa) are eventually deleted.
  142. But this idiom has got nothing to do with exception handling.
  143. And that is why I have been suggesting the following:
  144.  
  145.    + The job of auto_ptr should be to assist in exception handling
  146.      situations. An auto_ptr without copy semantics serves just
  147.      fine in all normal exception handling situations. So remove
  148.      the copy semantics from auto_ptr and make it a much safer
  149.      class for programmers to use by removing the possibility of
  150.      dangling pointers.
  151.  
  152.    + Create a separate taligent_ptr class to assist in situations
  153.      in which resource ownership is being transferred. This class
  154.      could open up the possibility of dangling pointers, but the
  155.      programmer would have the choice of using it or not.
  156.  
  157. The current auto_ptr class is trying to solve loosely related, but
  158. fundamentally different, problems at the same time. Breaking up
  159. the unnecessary coupling of the functionality can only help to
  160. make auto_ptr safer to use.
  161.  
  162. Note: Greg Colvin's article in February mentioned that the initial
  163.       impetus for the copy semantics for the standard auto_ptr came
  164.       from experiences reported from Taligent. Obviously, Taligent's
  165.       experiences are not unique. Other have reported that they have
  166.       used the idiom, and so have I. But it is nice to be able to
  167.       give an unambigious name to an idiom to make it easier to talk
  168.       about, and "Taligent idiom" and "taligent_ptr" just happen to
  169.       be convenient names for me to use. Nothing more should be
  170.       inferred from the use of the Taligent name.
  171.  
  172. -- 
  173. Ajay Kamdar        |    Email: ajay@lehman.com    |    Standard Disclaimer 
  174. Lehman Brothers    |    Phone: (201) 524-5048     |
  175.  
  176.  
  177. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  178. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  179. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  180. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  181. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  182.